package observer.jdk;


import java.util.Observable;
import java.util.Observer;

/**
 * 一个第三方的观察者，可能是百度，腾讯，微博等
 * 只给天气数据，只给用户名
 */
public class ThirdPartObserver implements Observer {

    //将被观察者传递回来这样写，并不是多此一举，而是这样观察者就知道是哪个被观察者通知它的，也可以通过这个参数直接获取或操作被观察者的状态。
    @Override
    public void update(Observable o, Object arg) {
        if (o instanceof WeatherData) {
            WeatherData weatherData = (WeatherData) o;
            float temperature = weatherData.getTemperature();
            float pressure = weatherData.getPressure();
            float humidity = weatherData.getHumidity();
            if (((Boolean) arg)) {
                temperature = ConversionUtil.celsiusToFahrenheit(temperature);
                pressure = ConversionUtil.hPaToInHg(pressure);
                humidity = ConversionUtil.humidityToPercentage(humidity);
            }
            System.out.println("Temperature: " + temperature);
            System.out.println("Pressure: " + pressure);
            System.out.println("Humidity: " + humidity);
        } else if (o instanceof UserInfoData) {
            UserInfoData userInfoData = (UserInfoData) o;
            System.out.println("username: " + userInfoData.getUsername());
        }
    }
}


class ConversionUtil {

    // 摄氏度转华氏度
    public static float celsiusToFahrenheit(float celsius) {
        return (celsius * 9 / 5) + 32;
    }

    // 气压（百帕）转英寸汞柱
    public static float hPaToInHg(float hPa) {
        return hPa * 0.0295301f;
    }

    // 湿度转百分比
    public static float humidityToPercentage(float humidity) {
        return humidity * 100;
    }
}
